package io.renren.sf.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.dao.CbChapterListDao;
import io.renren.sf.dao.CbChapterPictureDao;
import io.renren.sf.entity.CbChapterPictureEntity;
import io.renren.sf.vo.BookToUnitVo;
import io.renren.sf.vo.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.sf.dao.CbChapterDao;
import io.renren.sf.entity.CbChapterEntity;
import io.renren.sf.service.CbChapterService;
import org.springframework.util.CollectionUtils;


@Service("cbChapterService")
public class CbChapterServiceImpl extends ServiceImpl<CbChapterDao, CbChapterEntity> implements CbChapterService {

    @Autowired
    private CbChapterListDao cbChapterListDao;

    @Autowired
    private CbChapterPictureDao cbChapterPictureDao;

    @Override
    public Page<ChapterVo> getChapterByBookId(Map<String, Object> params) {
        String bookId = (String) params.get("bookId");
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        return cbChapterListDao.getChapterByBookId(new Page<ChapterVo>(page, limit), new QueryWrapper<ChapterVo>().eq("c.bid", Long.parseLong(bookId)));
    }

    @Override
    public void saveChapterAndPic(Map<String, Object> params) {
        List<String> chapterPics = (List<String>) params.get("chapterPics");
        String bookId = (String) params.get("bookId");
        String name = (String) params.get("name");
        // 添加章节信息
        CbChapterEntity chapterEntity = new CbChapterEntity();
        //chapterEntity.setBid(Long.parseLong(bookId));
        chapterEntity.setCorder((Integer) params.get("chapterOrder"));
        chapterEntity.setName(name);
        baseMapper.insert(chapterEntity);
        // 没有图片直接return
        if(CollectionUtils.isEmpty(chapterPics)){
            return;
        }
        // 添加章节图片
        List<CbChapterPictureEntity> chapterPictureEntities = new ArrayList<>();
        for (int i = 0; i < chapterPics.size(); i++) {
            CbChapterPictureEntity pictureEntity = new CbChapterPictureEntity();
            pictureEntity.setId(IdWorker.getId());
            pictureEntity.setCid(chapterEntity.getId());
            pictureEntity.setChapterPicture(chapterPics.get(i));
            pictureEntity.setCorder(i + 1);
            chapterPictureEntities.add(pictureEntity);
        }
        cbChapterPictureDao.insertBatch(chapterPictureEntities);
    }

    @Override
    public List<BookToUnitVo> getBookAndUnitByEditionId(Long editionId) {
        return baseMapper.getBookAndUnitByEditionId(editionId);
    }

    @Override
    public List<CbChapterEntity> getChapterAndPicAndWord(long uid) {
        return baseMapper.getChapterAndPicAndWord(uid);
    }
}